<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="单据编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据状态" prop="type">
        <el-select v-model="queryParams.status" placeholder="请选择单据状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="updateAllStatus"
          >审核</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据编号" align="center" prop="orderNumber" />
      <el-table-column
        label="上游单据编号"
        align="center"
        prop="parentOrderNumber"
      />
      <el-table-column label="单据类型" align="center" prop="typeStr" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status === '0'">未审核</div>
          <div v-if="scope.row.status === '1'">已审核</div>
        </template>
      </el-table-column>
      <el-table-column label="企业" align="center" prop="enterpriseName" />
      <el-table-column
        label="单据时间"
        align="center"
        prop="orderTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="code" />
      <el-table-column label="物料名称" align="center" prop="name" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="承诺到货时间" align="center" prop="commitmentTime" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="是否已下推" align="center" prop="isPush">
        <template slot-scope="scope">
          <div v-if="scope.row.isPush === '0'">未下推</div>
          <div v-if="scope.row.isPush === '1'">已下推</div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status == '0'"
            @click="updateOneStatus(scope.row)"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.isPush == '0'"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="物料编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  addOrder,
  updateOrder,
  updateAllOrder,
  delOrder,
} from "@/api/system/order";

export default {
  name: "BjOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 单据表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        type: "3",
        status: null,
        sid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [{ required: true, message: "请输入物料名称", trigger: "blur" }],
        name: [{ required: true, message: "请输入物料编码", trigger: "blur" }],
        num: [
          { required: true, message: "请输入采购数量", trigger: "blur" },
          { pattern: /^[1-9]\d*$/, message: "请输入采购数量", trigger: "blur" },
        ],
      },
      // 单据状态
      options: [
        {
          value: "0",
          label: "未审核",
        },
        {
          value: "1",
          label: "已审核",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询单据列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        parentOrderNumber: null,
        type: null,
        status: "0",
        eid: null,
        orderTime: null,
        code: null,
        name: null,
        num: null,
        price: null,
        sid: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length === 0;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单据";
    },
    /** 审核 */
    updateOneStatus(row) {
      let obj = {
        id: row.id,
        status: "1",
      };
      updateOrder(obj).then((response) => {
        if (response.code == 200) {
          this.$message({
            showClose: true,
            message: "审核成功",
            type: "success",
          });
        }
        this.getList();
      });
    },
    updateAllStatus() {
      let obj = {
        ids: this.ids,
      };
      this.$modal
        .confirm("是否确认审核通过数据项？")
        .then(function () {
          return updateAllOrder(obj);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("审核成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除单据编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
